#! /usr/bin/perl 

#-------------------------------------------------
# Script to parsing HMA input data.
#
# Usage:
#    read_hma_insurance0.pl  data_file
#    _
#-------------------------------------------------

use strict;
use Time::Local;

#-------------------------
# This may grow
#-------------------------
our @covered_states = (
		'AL','AR','FL','GA','KY','MO','MS',
		'NC','OK','PA','SC','TN','TX', 'WA','WV');

#---------------------------------------------------
# Fields of table 'read_insurance0_new_t' in PayorIntel 
#---------------------------------------------------
our @payor_read_ins1 = qw(
  cust_id
  treatment_code
  output_id
  hid
  acctnum
  cpi
  client_npi1
  npi1
  tax_id1
  legacy1
  pin1
  entity_name1
  entity_pay_to_street1
  entity_pay_to_city1
  entity_pay_to_state1
  entity_pay_to_zip1
  entity_information1
  entity_phone1
  lname
  mname
  fname
  addr1
  addr2
  city
  state
  zip5
  zip4
  ssn
  gender
  dob
  g_lname
  g_mname
  g_fname
  g_addr1
  g_addr2
  g_city
  g_state
  g_zip5
  g_zip4
  g_ssn
  g_gender
  g_dob
  relationship_flag
  g_employ_name
  provider_state
  service_date_begin
  service_date_end
  pat_proc_type
  pat_type
  cpt
  drg
  physician_code
  charges
  balance
  acct_status
  client_field1
  client_field2
  client_field3
  client_date1
);


#----------------------------------------------
# Processing input file or input file handle
#----------------------------------------------
our $indata = $ARGV[0] || "-";
our $INFD = undef;
our %dest;

if( $indata ne "-" ) {
	if ( ! -r $indata ) {
		print STDERR " file $indata not accessable !!!\n";
		exit 1;
	}
	else {
		open(HMA, "<$indata");
		$INFD = *HMA;
	}
} else {
	$INFD = *STDIN;
}



#-------------------------------------------------
# processing data record by record
#-------------------------------------------------

while(my $line = <$INFD> ) {
	chomp $line;
	$line =~ s/[^\x20-\x7e]//g;
	next if $line =~ /^\s*$/;
	my @fields = split( /\|/, $line);
	$dest{hid}     = $fields[0];
	$dest{cpi}     = $fields[1];
	$dest{acctnum} = $fields[2];
	$dest{lname}   = $fields[4];
	$dest{fname}   = $fields[5];
	$dest{mname}   = $fields[6];
	$dest{dob}     = $fields[7];
	$dest{ssn}     = $fields[8];
	$dest{gender}  = $fields[9];
	$dest{addr1}   = $fields[10];
	$dest{addr2}   = $fields[11];
	$dest{city}    = $fields[12];
	$dest{state}   = $fields[13];
	$dest{zip5}    = $fields[14];
	$dest{service_date_begin} = $fields[21];
	$dest{service_date_end}   = $fields[23];
	$dest{charges} = $fields[24];
	$dest{balance} = $fields[25];
	$dest{pat_type} =$fields[31];
	$dest{pat_proc_type} = $fields[32];
	$dest{g_employ_name} = $fields[57];
	$dest{g_lname} = $fields[65];
	$dest{g_fname} = $fields[66];
	$dest{g_mname} = $fields[67];
	$dest{g_ssn}   = $fields[68];
	$dest{g_gender}= $fields[69];
	$dest{g_addr1} = $fields[72];
	$dest{g_addr2} = $fields[73];
	$dest{g_city}  = $fields[74];
	$dest{g_state} = $fields[75];
	$dest{g_zip5}  = $fields[76];
        my $fin_class   = $fields[28];
	my $payor_plan1 = $fields[34];
	my $payor_plan2 = $fields[35];
	my $payor_plan3 = $fields[36];
	my $temp_g_dob  = $fields[70];
	@fields=();		# release memory.

	$dest{lname}   =~ s/,.*$//;
	$dest{g_lname} =~ s/,.*$//;
	$dest{fname}   =~ s/\s.*$//;
	$dest{g_fname} =~ s/\s.*$//;

	$dest{cust_id}	=49;   # HMA's id
	$dest{treatment_code}=1;
	$dest{output_id}=1;


	if( $temp_g_dob =~ m#/# ) {
		$dest{g_dob} = format_dob($temp_g_dob);
	}

	$dest{provider_state} = get_state($dest{hid});
	if( $dest{provider_state} =~ /^\s*$/ ) {
		$dest{provider_state} = $dest{state};
	}

	$dest{client_npi1}=1;
	foreach my $s ( @covered_states ) {
		if($dest{state} eq $s ) {
			$dest{client_npi1} = $s;
		}
	}

	my $payor_ind = 0;
	if( $payor_plan1 =~ /^(000|100|\s*)$/ ) {
		$payor_ind ++;
	}
	if( $payor_plan2 =~ /^(000|100|\s*)$/ ) {
		$payor_ind ++;
	}
	if( $payor_plan3 =~ /^(000|100|\s*)$/ ) {
		$payor_ind ++;
	}
        if( $fin_class =~ /^P/ ) {
                $payor_ind ++;
        }

        if ( $payor_ind == 4 ) {

                if    ( $dest{hid} == 824)  { $dest{output_id} = 2;}
                elsif ( $dest{hid} == 858)  { $dest{output_id} = 3;}
                elsif ( $dest{hid} == 813)  { $dest{output_id} = 4;}
                elsif ( $dest{hid} == 831)  { $dest{output_id} = 5;}
                elsif ( $dest{hid} == 820)  { $dest{output_id} = 6;}
                elsif ( $dest{hid} == 875)  { $dest{output_id} = 7;}
                elsif ( $dest{hid} == 833)  { $dest{output_id} = 8;}
                elsif ( $dest{hid} == 847)  { $dest{output_id} = 9;}
                elsif ( $dest{hid} == 852)  { $dest{output_id} = 10;}
                elsif ( $dest{hid} == 805)  { $dest{output_id} = 11;}
                elsif ( $dest{hid} == 856)  { $dest{output_id} = 12;}
                elsif ( $dest{hid} == 825)  { $dest{output_id} = 13;}
                elsif ( $dest{hid} == 868)  { $dest{output_id} = 14;}
                elsif ( $dest{hid} == 870)  { $dest{output_id} = 15;}
                elsif ( $dest{hid} == 864)  { $dest{output_id} = 16;}
                elsif ( $dest{hid} == 888)  { $dest{output_id} = 17;}
                elsif ( $dest{hid} == 884)  { $dest{output_id} = 18;}
                elsif ( $dest{hid} == 886)  { $dest{output_id} = 19;}
                elsif ( $dest{hid} == 878)  { $dest{output_id} = 20;}
                elsif ( $dest{hid} == 889)  { $dest{output_id} = 21;}
                elsif ( $dest{hid} == 876)  { $dest{output_id} = 22;}
                elsif ( $dest{hid} == 843)  { $dest{output_id} = 23;}
                elsif ( $dest{hid} == 883)  { $dest{output_id} = 24;}
                elsif ( $dest{hid} == 851)  { $dest{output_id} = 25;}
                elsif ( $dest{hid} == 817)  { $dest{output_id} = 26;}
                elsif ( $dest{hid} == 857)  { $dest{output_id} = 27;}
                elsif ( $dest{hid} == 834)  { $dest{output_id} = 28;}
                elsif ( $dest{hid} == 862)  { $dest{output_id} = 29;}
                elsif ( $dest{hid} == 879)  { $dest{output_id} = 30;}
                elsif ( $dest{hid} == 810)  { $dest{output_id} = 31;}
                elsif ( $dest{hid} == 844)  { $dest{output_id} = 32;}
                elsif ( $dest{hid} == 853)  { $dest{output_id} = 33;}
                elsif ( $dest{hid} == 832)  { $dest{output_id} = 34;}
                elsif ( $dest{hid} == 850)  { $dest{output_id} = 35;}
                elsif ( $dest{hid} == 804)  { $dest{output_id} = 36;}
                elsif ( $dest{hid} == 854)  { $dest{output_id} = 37;}
                elsif ( $dest{hid} == 711)  { $dest{output_id} = 38;}
                elsif ( $dest{hid} == 890)  { $dest{output_id} = 39;}
                elsif ( $dest{hid} == 837)  { $dest{output_id} = 40;}
                elsif ( $dest{hid} == 874)  { $dest{output_id} = 41;}
                elsif ( $dest{hid} == 829)  { $dest{output_id} = 42;}
                elsif ( $dest{hid} == 841)  { $dest{output_id} = 43;}
                elsif ( $dest{hid} == 842)  { $dest{output_id} = 44;}
                elsif ( $dest{hid} == 848)  { $dest{output_id} = 45;}
                elsif ( $dest{hid} == 819)  { $dest{output_id} = 46;}
                elsif ( $dest{hid} == 814)  { $dest{output_id} = 47;}
                elsif ( $dest{hid} == 822)  { $dest{output_id} = 48;}
                elsif ( $dest{hid} == 816)  { $dest{output_id} = 49;}
                elsif ( $dest{hid} == 821)  { $dest{output_id} = 50;}
                elsif ( $dest{hid} == 846)  { $dest{output_id} = 51;}
                elsif ( $dest{hid} == 867)  { $dest{output_id} = 52;}
                elsif ( $dest{hid} == 855)  { $dest{output_id} = 53;}
                elsif ( $dest{hid} == 869)  { $dest{output_id} = 54;}
                elsif ( $dest{hid} == 803)  { $dest{output_id} = 55;}
                elsif ( $dest{hid} == 827)  { $dest{output_id} = 56;}
                elsif ( $dest{hid} == 812)  { $dest{output_id} = 56;}
                elsif ( $dest{hid} == 863)  { $dest{output_id} = 57;}
                elsif ( $dest{hid} == 710)  { $dest{output_id} = 58;}
                elsif ( $dest{hid} == 823)  { $dest{output_id} = 59;}
                elsif ( $dest{hid} == 835)  { $dest{output_id} = 60;}
                elsif ( $dest{hid} == 861)  { $dest{output_id} = 61;}
                elsif ( $dest{hid} == 808)  { $dest{output_id} = 62;}

	        print_record(\%dest); 
        }
        
	%dest=();
}


exit 0;


sub print_record($){
	my $hr = shift;
	#------------------------------------------
	# Cases not to print record.
	#------------------------------------------
	return if $hr->{balance} == 0.00;

	foreach my $fld ( @payor_read_ins1 ) {
		my $x = $hr->{$fld};
		$x =~ s/^\s*//;
		$x =~ s/\s*$//;
		$x =~ s/\|//;
		print "$x";
		print "|" if $fld ne $payor_read_ins1[$#payor_read_ins1];
	}
	print "\n";
}

sub format_dob($) {
	my $date = shift;
	my $ret = $date;
	if( $date =~ m{^(\d\d)/(\d\d)/(\d\d)$} ) {
		my $m = $1;
		my $d = $2;
		my $y = $3;
		
		$d =~ s/^0//;
		$m =~ s/^0//;
		$y =~ s/^0//;

		my ($nd, $nm, $ny) = (localtime)[3,4,5];
		if ( 100+$y > $ny  ) {	
				$ret = sprintf("%02d/%02d/%02d", $m, $d, 1900+$y);
		} else {
				$ret = sprintf("%02d/%02d/%02d", $m, $d, 2000+$y);
		}
	}
	$ret;
}

#-------------------------------------------------
# get_state(hid)
#      Convert 'hid' to state short name.
#-------------------------------------------------
sub get_state($) {
	my $hid = shift;
	my $stt;

	if( $hid =~ /^(820|824)$/ ) 	 
		{ $stt = 'AL'}
	elsif($hid =~ /^(825|831|836)$/) 
		{ $stt = 'AR'}
	elsif($hid =~ /^(803|808|812|814|816|817|819|
					 821|823|827|835|843|844|846|
					 851|853|855|857|861|862|863|
					 865|867|869|880|883)$/x )
		{ $stt = 'FL'}
	elsif($hid =~ /^(804|832|879)$/) 
		{ $stt = 'GA'}
	elsif($hid =~ /^810/)  			 
		{ $stt = 'KY'}
	elsif($hid =~ /^(886|888)/)   	 
		{ $stt = 'MO'}
	elsif($hid =~ /^(822|829|837|838|841|842|848|
					 854|874|890)$/x)
		{ $stt = 'MS'}
	elsif($hid =~ /^(805|826|840|856|875)$/) 
		{ $stt = 'NC'}
	elsif($hid =~ /^(834|870|871)$/) 	
		{ $stt = 'OK'}
	elsif($hid =~ /^(847|852|858)$/)
		{ $stt = 'PA'}
	elsif($hid =~ /^(813|833|860)$/)
		{ $stt = 'SC'}
	elsif($hid =~ /^(864|884|885|889)$/)
		{ $stt = 'TN'}
	elsif($hid =~ /^(868|872)$/)
		{ $stt = 'TX'}
	elsif($hid =~ /^(876|878)$/)
		{ $stt = 'WA'}
	elsif($hid =~ /^(850)/ )
		{ $stt = 'WV'}
	else { $stt = ""}

	$stt;
}

